<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>


    </style>
</head>
<body>
    <input type="text" id="inp">
    <input type="checkbox" name="" id="is-record">
    记住用户名

    <script>

        const inp = document.querySelector('#inp');
        const isRecord = document.querySelector('#is-record');

        // 页面刷新后的显示
        // 返显
        inp.value = localStorage.getItem('username') || '';
        isRecord.checked = !!localStorage.getItem('username'); // !!转布尔值

        isRecord.addEventListener('change', function () {
            if (this.checked) {
                localStorage.setItem('username', inp.value);
            } else {
                localStorage.removeItem('username');
            }
        });

        inp.addEventListener('input', function () {
            if (isRecord.checked) {
                localStorage.setItem('username', inp.value);
            }
        });

    </script>
</body>
</html>